<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户id</label>
        <div class="layui-input-inline">
            <input type="text" name="userId" lay-verify="required" lay-reqtext="用户id不能为空" placeholder="请填写用户id" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">优惠券金额</label>
        <div class="layui-input-inline">
            <input type="text" name="couponAmount" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" lay-verify="required" lay-reqtext="优惠券金额不能为空" placeholder="请填写优惠券金额" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">优惠券类型</label>
        <div class="layui-input-block">
            <select name="couponType" lay-filter="couponType">
                <option value=''>请选择</option>
                <option value='1'>通用券</option>
                <option value='0'>单品券</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item" id="no" style="display: none">
        <label class="layui-form-label">商品编号</label>
        <div class="layui-input-inline">
            <input type="text" name="goodsNo" placeholder="请填写商品编号" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认发放</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>

<script>
    window.onload = function(){

        layui.use(['layer', 'form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            // 监听优惠券类型下拉选择
            form.on('select(couponType)', function (dataObj) {
                let couponType = $('select[name="couponType"]').find("option:selected").val();
                if (couponType === '0') {// 单品券
                    $("#no").show();
                } else if (couponType === '1') {// 通用券
                    $("#no").hide();
                } else {
                    $("#no").hide();
                }
            });

            // 监听提交
            form.on('submit(saveBtn)', function (data) {

                // 单品券时，商品编号必填
                let couponType = $('select[name="couponType"]').find("option:selected").val();
                if(!isNotNull(couponType)){
                    layer.msg("请选择优惠券类型！");
                    return
                }
                if (couponType === '0') {// 单品券
                    if(!isNotNull(data.field.goodsNo)){
                        layer.msg("请填写商品编号！");
                        return
                    }
                }

                var index = layer.alert("确定发放优惠券吗？", {
                    title: '提示'
                }, function () {
                    let myMsg = layer.msg("正在发放...",{
                        icon:16,
                        time:-1
                    })
                    $.ajax({
                        type:"post",
                        url:'/coupon/issue',
                        data:JSON.stringify(data.field),
                        dataType:'json',
                        contentType: 'application/json; charset=UTF-8',
                        success:function(req){
                            layer.msg(req.msg);
                            layer.close(index);
                            // 关闭弹出层
                            setTimeout(function () {
                                layer.close(myMsg);// 手动关闭
                                location.reload();// 刷新页面
                                /*let iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);*/
                            }, 1500);
                        },
                        error:function(e){
                            layer.msg(req.msg);
                            layer.close(myMsg);//手动关闭
                        }
                    })

                });

                return false;
            });

        });
    }

</script>
</body>
</html>
